<?php
include_once(dirname(__FILE__) . "/../../configure/configure.php");		//--> global var
include_once(dirname(__FILE__) . "/../configure/admin.config.inc.php");	//--> admin global var

include_once(dirname(__FILE__) . "/../tools/smarty/libs/Smarty.class.php");					//--> out template
include_once(dirname(__FILE__) . "/../../lib/impl/CouponImpl.php");
include_once(dirname(__FILE__) . "/../../lib/impl/MerchantImpl.php");
include_once(dirname(__FILE__) . "/../../lib/impl/CategoryImpl.php");

require_once(dirname(__FILE__) . "/../controlHeader.php");					//--> system control header
require_once(dirname(__FILE__) . "/../controlOperate.php");				//-->

$couponImpl	= new CouponImpl(DB_TAG_COUPON);			//--> Coupon
$merImpl 	= new MerchantImpl(DB_TAG_COUPON);			//--> Merchant
$catImpl 	= new CategoryImpl(DB_TAG_COUPON);			//--> 

$error_message = '';


if(isset($_GET['keyword'])){
	$keyword 	= $_GET['keyword'];
	$searchType = $_GET['searchType'];
	
	if(empty($keyword) && $searchType != 'Recommended'){
		$error_message	= 'Keyword is required.';
	}else{
		if($searchType == 'id'){
			if(is_numeric($keyword))
				$where	= " WHERE id = $keyword";
			else
				$error_message	= 'Please input coupon id';
		}else if($searchType == 'merId'){
			if(is_numeric($keyword))
				$where	= " WHERE merchantId = $keyword";
			else
				$error_message	= 'Please input merchant id';		
		}else if($searchType == 'code'){
			$where	= " WHERE code like '%$keyword%'";
		}else if($searchType == 'advertiserId'){
			$where	= " WHERE code like '%$keyword%'";
		}else if($searchType == 'Recommended'){
			$where	= " WHERE isRecommend = '1'";
			}else if($searchType == 'name'){
			$where	= " WHERE name like '%$keyword%'";
		}else{
			$where	= " WHERE name like '%$keyword%'";
		}
	}
}

if(isset($_GET['action'])){
	if($_GET['action'] == 'recommend'){
		$couponId 		= $_GET['id'];
		$isRecommend 	= '1';
		
		$couponImpl->setRecommend($couponId, $isRecommend);
	}else if($_GET['action'] == 'recommended'){
		$couponId 		= $_GET['id'];
		$isRecommend 	= '0';
		
		$couponImpl->setRecommend($couponId, $isRecommend);
	}
}
	
$order = ' ORDER BY isRecommend DESC, createDate DESC';


$oCoupons	= array();

$arrAllDataList = $couponImpl->getCustoms($where, $order, $page, DISPLAY_DATA_SIZE);
foreach($arrAllDataList as $oCoupon){
	
	$oMer = $merImpl->getById($oCoupon->getMerchantId());
	$oCoupon->setMerchant($oMer);
	
	$catId = $couponImpl->getCategoryId($oCoupon->getId());
	
	$oCat = $catImpl->getById($catId);
	$oCoupon->setCategory($oCat);
		
	$oCoupons[]	= $oCoupon;
}

$arrSearchType = array('id' => 'Coupon Id', 'name' => 'Coupon Name', 'code' => 'Coupon Code', 'merId' => 'Merchant ID', 'advertiserId' => 'Advertiser Id', 'Recommended' => 'Recommended');


/*----- out html -----*/
$smarty = new Smarty();					//----- out template
$smarty->template_dir = TEMPLATE_SYS_DIR;
$smarty->compile_dir  = COMPILE_SYS_DIR;

$smarty->assign('error_message', $error_message);
$smarty->assign('menuGid', $menuGid);
$smarty->assign('menuid', $menuid);

$smarty->assign('arrSearchType', $arrSearchType);
$smarty->assign('searchTypeS', $searchType);
$smarty->assign('keyword', $keyword);

$smarty->assign('selfFileName', $selfFileName);
$smarty->assign('arrOperateInfo', $arrOperateInfo);

$smarty->assign('oCoupons', $oCoupons);

$smarty->assign('PAGE_BAR', $couponImpl->pagenav);

$smarty->display('listCoupon.htm');
?>
